//
// AOperand.cpp for  in /home/seth/svn/vmtmp
// 
// Made by kevin lansel
// Login   <lansel_k@epitech.net>
// 
// Started on  Tue Feb 19 20:18:12 2013 kevin lansel
// Last update Tue Feb 19 22:00:46 2013 kevin lansel
//

#include	<cstdlib>
#include	"AOperand.hpp"
#include	"Int8.hpp"
#include	"Int16.hpp"
#include	"Int32.hpp"
#include	"Float.hpp"
#include	"Double.hpp"

AOperand::AOperand(const std::string &str, eOperandType type) : IOperand()
{
  this->_func_type = new func[5];

  this->_func_type[0].type = INT8;
  this->_func_type[0].function = &AOperand::createInt8;
  this->_func_type[1].type = INT16;
  this->_func_type[1].function = &AOperand::createInt16;
  this->_func_type[2].type = INT32;
  this->_func_type[2].function = &AOperand::createInt32;
  this->_func_type[3].type = FLOAT;
  this->_func_type[3].function = &AOperand::createFloat;
  this->_func_type[4].type = DOUBLE;
  this->_func_type[4].function = &AOperand::createDouble;
  this->_type = type;
  this->_str = str;
}


AOperand::~AOperand()
{
  delete[] this->_func_type;
}

std::string const &AOperand::toString() const
{
  return (this->_str);
}

IOperand	*AOperand::createOperand(eOperandType type, const std::string &value)
{
  int	i;

  i = 0;
  while (i < 5)
    {
      if (type == this->_func_type[i].type)
	return (this->_func_type[i].*function(value));
      i++;
    }
  return (NULL);
}

IOperand	*AOperand::createInt8(const std::string & value)
{
  return (new Int8(static_cast<char>(atoi(value.c_str())), value));
}

IOperand	*AOperand::createInt16(const std::string & value)
{
  return (new Int16(static_cast<short>(atoi(value.c_str())), value));
}

IOperand	*AOperand::createInt32(const std::string & value)
{
  return (new Int32(atoi(value.c_str()), value));
}

IOperand	*AOperand::createFloat(const std::string & value)
{
  return (new Float(static_cast<float>(atof(value.c_str())) , value));
}

IOperand	*AOperand::createDouble(const std::string & value)
{
  return (new Double(atof(value.c_str()), value));
}
